package com.ruoyi.callback.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.callback.service.IteamService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.modules.domain.TransportationCompany;
import com.ruoyi.modules.service.ITransportationCompanyService;
import com.ruoyi.region.domain.TSysArea;
import com.ruoyi.region.domain.TSysCity;
import com.ruoyi.region.domain.TSysProvince;
import com.ruoyi.region.service.ITSysAreaService;
import com.ruoyi.region.service.ITSysCityService;
import com.ruoyi.region.service.ITSysProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @date 2024/12/28 11:19
 * @desc 车队回调 实现类
 **/
@Service
public class IteamServiceImpl implements IteamService {


    @Autowired
    private ITransportationCompanyService transportationCompanyService;

    @Autowired
    private ITSysProvinceService tSysProvinceService;
    @Autowired
    private ITSysAreaService tSysAreaService;

    @Autowired
    private ITSysCityService tSysCityService;

    /**
     * 车队-->添加回调
     */

//    {
//        "atime": "2024-12-27 19:59:57",
//            "auser": "admin_gj",
//            "banNum": 0,
//            "carNum": 0,
//            "cmpId": "7",
//            "delNum": 0,
//            "des": "36",
//            "email": "35",
//            "exts": "{\"old_team_id\":\"12\",\"user_id\":\"2\",\"name\":\"3\",\"short_name\":\"4\",\"enterprise_type\":\"5\",\"jurisdiction\":\"6\",\"enterprise_number\":\"7\",\"legal_person\":\"8\",\"max_car_num\":\"9\",\"vehicle_management_contact\":\"10\",\"vehicle_management_contact_phone\":\"11\",\"road_transport_permit_number\":\"12\",\"construction_waste_registration_\":\"13\",\"valid_until\":\"14\",\"social_credit_code\":\"15\",\"legal_person_id_attachment\":\"16\",\"qualification_certificate_attach\":\"17\",\"business_license_attachment\":\"18\",\"other_attachment\":\"19\",\"exts\":\"20\",\"address\":\"21\",\"pic\":\"22\",\"pic_contact\":\"23\",\"des\":\"24\",\"province_code\":\"25\",\"province_name\":\"26\",\"city_code\":\"27\",\"city_name\":\"28\",\"area_code\":\"29\",\"area_name\":\"30\",\"town_name\":\"31\",\"town_code\":\"32\"}",
//            "id": "4458",
//            "linkman": "33",
//            "maxCarNum": "{\"num\":1,\"2G\":2,\"4G\":3,\"28181G\":4}",
//            "name": "车队",
//            "noAuth": false,
//            "phone": "34",
//            "pid": "4327",
//            "seq": 10715086071862673000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
//            "videoNum": 0,
//            "zcode": "13_河北省"
//    }

    //130123_中国 河北省 石家庄市 正定县

    @Override
    public void addTeamCallback(String str) {
        JSONObject jsonObject = JSONObject.parseObject(str);
        // 车队id
        String teamId = jsonObject.getString("id");
        TransportationCompany transport = new TransportationCompany();
        transport.setOldTeamId(teamId);
        List<TransportationCompany> list = transportationCompanyService.selectTransportationCompanyList(transport);
        if(list.size()>0){
            System.out.println("车队已存在, 车队id:"+teamId+"请勿重复添加");
        }else{
            TransportationCompany team = this.changeDataTeam(jsonObject,str);
            int i = transportationCompanyService.insertTransportationCompany(team);
            if(i>0){
                System.out.println("添加车队成功，车队id: "+teamId);
            }else{
                System.out.println("添加车队失败，车队id: "+teamId);
            }
        }

    }

    /**
     * 车队-->修改回调
     * @param str
     */
//    {"atime":"2024-12-27 19:59:57","auser":"admin_gj","banNum":0,"carNum":0,"cmpId":"7","delNum":0,"des":"36","email":"35","exts":"{\"old_team_id\":\"12\",\"user_id\":\"2\",\"name\":\"3\",\"short_name\":\"4\",\"enterprise_type\":\"5\",\"jurisdiction\":\"6\",\"enterprise_number\":\"7\",\"legal_person\":\"8\",\"max_car_num\":\"9\",\"vehicle_management_contact\":\"10\",\"vehicle_management_contact_phone\":\"11\",\"road_transport_permit_number\":\"12\",\"construction_waste_registration_\":\"13\",\"valid_until\":\"14\",\"social_credit_code\":\"15\",\"legal_person_id_attachment\":\"16\",\"qualification_certificate_attach\":\"17\",\"business_license_attachment\":\"18\",\"other_attachment\":\"19\",\"exts\":\"20\",\"address\":\"21\",\"pic\":\"22\",\"pic_contact\":\"23\",\"des\":\"24\",\"province_code\":\"25\",\"province_name\":\"26\",\"city_code\":\"27\",\"city_name\":\"28\",\"area_code\":\"29\",\"area_name\":\"30\",\"town_name\":\"31\",\"town_code\":\"32\"}","id":"4458","linkman":"33","maxCarNum":"{\"num\":1,\"2G\":2,\"4G\":3,\"28181G\":4}","name":"车队","noAuth":false,"phone":"34","pid":"4327","seq":1.0715086071862673E301,"videoNum":0,"zcode":"13_河北省"}
    @Override
    public void updateTeamCallback(String str) {
        JSONObject jsonObject = JSONObject.parseObject(str);
        // 车队id
        String teamId = jsonObject.getString("id");
        TransportationCompany transport = new TransportationCompany();
        transport.setOldTeamId(teamId);
        List<TransportationCompany> list = transportationCompanyService.selectTransportationCompanyList(transport);
        if(list.size()>0){
            TransportationCompany team = this.changeDataTeam(jsonObject,str);
            team.setId(list.get(0).getId());
            int i = transportationCompanyService.updateTransportationCompany(team);
            if(i>0){
                System.out.println("车队修改成功, 主键id:"+list.get(0).getId());
            }else{
                System.out.println("车队修改失败, 主键id:"+list.get(0).getId());
            }
        }else{
            System.out.println("车队不存在, 车队id:"+teamId+"请勿修改");
        }

    }

    /**
     * 车队-->删除回调
     */
   // {"atime":"2024-12-27 19:59:57","auser":"admin_gj","banNum":0,"carNum":0,"cmpId":"7","delNum":0,"des":"36","email":"35","exts":"{\"old_team_id\":\"12\",\"user_id\":\"2\",\"name\":\"3\",\"short_name\":\"4\",\"enterprise_type\":\"5\",\"jurisdiction\":\"6\",\"enterprise_number\":\"7\",\"legal_person\":\"8\",\"max_car_num\":\"9\",\"vehicle_management_contact\":\"10\",\"vehicle_management_contact_phone\":\"11\",\"road_transport_permit_number\":\"12\",\"construction_waste_registration_\":\"13\",\"valid_until\":\"14\",\"social_credit_code\":\"15\",\"legal_person_id_attachment\":\"16\",\"qualification_certificate_attach\":\"17\",\"business_license_attachment\":\"18\",\"other_attachment\":\"19\",\"exts\":\"20\",\"address\":\"21\",\"pic\":\"22\",\"pic_contact\":\"23\",\"des\":\"24\",\"province_code\":\"25\",\"province_name\":\"26\",\"city_code\":\"27\",\"city_name\":\"28\",\"area_code\":\"29\",\"area_name\":\"30\",\"town_name\":\"31\",\"town_code\":\"32\"}","id":"4458","linkman":"33","maxCarNum":"{\"num\":1,\"2G\":2,\"4G\":3,\"28181G\":4}","name":"车队","noAuth":false,"phone":"34","pid":"4327","seq":1.0715086071862673E301,"videoNum":0,"zcode":"13_河北省"}
    @Override
    public void delTeamCallback(String str) {
        JSONObject jsonObject = JSONObject.parseObject(str);
        // 车队id
        String teamId = jsonObject.getString("id");
        TransportationCompany transport = new TransportationCompany();
        transport.setOldTeamId(teamId);
        List<TransportationCompany> list = transportationCompanyService.selectTransportationCompanyList(transport);
        if(list.size()>0){
            int i = transportationCompanyService.deleteTransportationCompanyById(list.get(0).getId());
            if(i>0){
                System.out.println("车队删除成功, 主键id:"+list.get(0).getId());
            }else{
                System.out.println("车队删除失败, 主键id:"+list.get(0).getId());
            }
        }else{
            System.out.println("车队已删除, 车队id:"+teamId+"请勿删除");
        }
    }

    /**
     *  车队数据封装
     */
    private TransportationCompany changeDataTeam(JSONObject jsonObject,String str){
        TransportationCompany transportationCompany = new TransportationCompany();
        transportationCompany.setOldTeamId(jsonObject.getString("id")==null?"":jsonObject.getString("id"));// 原始车队id
        transportationCompany.setName(jsonObject.getString("name")==null?"":jsonObject.getString("name"));
        transportationCompany.setPic(jsonObject.getString("linkman")==null?"":jsonObject.getString("linkman"));
        transportationCompany.setPicContact(jsonObject.getString("phone")==null?"":jsonObject.getString("phone"));
        transportationCompany.setExts(jsonObject.getString("exts")==null?"":jsonObject.getString("exts"));
        transportationCompany.setContent(jsonObject.getString("des")==null?"":jsonObject.getString("des"));
        transportationCompany.setMaxCarNum(jsonObject.getString("maxCarNum")==null?"":jsonObject.getString("maxCarNum"));
        String strData = jsonObject.getString("atime");
        Date s = DateUtils.parseDate(strData);
        transportationCompany.setCreateTime(s);
        transportationCompany.setOtherAttachment(str);//其他-附件
        // 解析exts 数据
        String exts = jsonObject.getString("exts");
        if(StringUtils.hasText(exts)) {
            JSONObject jsonObject1 = JSONObject.parseObject(exts);
            transportationCompany.setAddress(jsonObject1.getString("address")==null?"":jsonObject1.getString("address"));
            transportationCompany.setRoadTransportPermitNumber(jsonObject1.getString("road_transport_permit_number")==null?"":jsonObject1.getString("road_transport_permit_number"));
            transportationCompany.setConstructionWasteRegistrationNumber(jsonObject1.getString("construction_waste_registration_")==null?"":jsonObject1.getString("construction_waste_registration_"));
            transportationCompany.setSocialCreditCode(jsonObject1.getString("social_credit_code")==null?"":jsonObject1.getString("social_credit_code"));
            transportationCompany.setLegalPerson(jsonObject1.getString("legal_person")==null?"":jsonObject1.getString("legal_person"));
            String validUntil = jsonObject1.getString("valid_until");
            if(StringUtils.hasText(validUntil)){

                Date validUntild = DateUtils.parseDate(validUntil);
                transportationCompany.setValidUntil(validUntild);
            }

        }
        String zcode = jsonObject.getString("zcode");
        if(StringUtils.hasText(zcode)){
            String[] split = zcode.split("_");
            //13_河北省
            //1301_石家庄市
            //130105_新华区
            String code =  split[0];
            String codeName =  split[1];
            Integer codeLength = code.length();
            // 省
            if(codeLength==2){
                    TSysProvince tSysProvince = new TSysProvince();
                    tSysProvince.setProvinceCode(code+"0000");
                    tSysProvince.setProvinceName(codeName);
                    List<TSysProvince> provinceList = tSysProvinceService.selectTSysProvinceList(tSysProvince);
                    if(provinceList.size()>0){
                        transportationCompany.setProvinceCode(provinceList.get(0).getProvinceCode());
                        transportationCompany.setProvinceName(provinceList.get(0).getProvinceName());
                    }
            }
            // 市
            if(codeLength ==4){
                TSysCity tSysCity = new TSysCity();
                tSysCity.setCityCode(code+"00");
                tSysCity.setCityName(codeName);
                List<TSysCity> cityList = tSysCityService.selectTSysCityList(tSysCity);
                if(cityList.size()>0){
                    transportationCompany.setCityCode(cityList.get(0).getCityCode());
                    transportationCompany.setCityName(cityList.get(0).getCityName());

                    // 省
                    // 逆推规则---->城市code查省名称和省code
                    TSysProvince tSysProvince = new TSysProvince();
                    tSysProvince.setProvinceCode(cityList.get(0).getProvinceCode());
                    List<TSysProvince> provinceList = tSysProvinceService.selectTSysProvinceList(tSysProvince);
                    if(provinceList.size()>0){
                        transportationCompany.setProvinceCode(provinceList.get(0).getProvinceCode());
                        transportationCompany.setProvinceName(provinceList.get(0).getProvinceName());
                    }
                }
            }
            // 区县
            if(codeLength ==6){
                // 区县
                TSysArea tSysArea = new TSysArea();
                tSysArea.setAreaCode(code);
                tSysArea.setAreaName(codeName);
                List<TSysArea> areaList = tSysAreaService.selectTSysAreaList(tSysArea);
                if(areaList.size()>0){
                    transportationCompany.setAreaCode(areaList.get(0).getAreaCode());
                    transportationCompany.setAreaName(areaList.get(0).getAreaName());

                    // 逆推规则---->区县code查城市名称和城市code
                    // 市
                    TSysCity tSysCity = new TSysCity();
                    tSysCity.setCityCode(areaList.get(0).getCityCode());
                    List<TSysCity> cityList = tSysCityService.selectTSysCityList(tSysCity);
                    if(cityList.size()>0){
                        transportationCompany.setCityCode(cityList.get(0).getCityCode());
                        transportationCompany.setCityName(cityList.get(0).getCityName());

                        // 逆推规则---->城市code查省名称和省code
                        // 省
                        TSysProvince tSysProvince = new TSysProvince();
                        tSysProvince.setProvinceCode(cityList.get(0).getProvinceCode());
                        List<TSysProvince> provinceList = tSysProvinceService.selectTSysProvinceList(tSysProvince);
                        if(provinceList.size()>0){
                            transportationCompany.setProvinceCode(provinceList.get(0).getProvinceCode());
                            transportationCompany.setProvinceName(provinceList.get(0).getProvinceName());
                        }
                    }

                }

            }


//            for (int i = 0; i < split.length; i++) {
//                System.out.println(split[i]);
//                // 省
//                if(i==0){
//                    TSysProvince tSysProvince = new TSysProvince();
//                    tSysProvince.setProvinceName(split[i]);
//                    List<TSysProvince> provinceList = tSysProvinceService.selectTSysProvinceList(tSysProvince);
//                    if(provinceList.size()>0){
//                        transportationCompany.setProvinceCode(provinceList.get(0).getProvinceCode());
//                        transportationCompany.setProvinceName(provinceList.get(0).getProvinceName());
//                    }
//                }else if(i==1){ //市
//                    TSysCity tSysCity = new TSysCity();
//                    tSysCity.setCityName(split[i]);
//                    List<TSysCity> cityList = tSysCityService.selectTSysCityList(tSysCity);
//                    if(cityList.size()>0){
//                        transportationCompany.setCityCode(cityList.get(0).getCityCode());
//                        transportationCompany.setCityName(cityList.get(0).getCityName());
//                    }
//
//                }else if(i==2){//区
//                    TSysArea tSysArea = new TSysArea();
//                    tSysArea.setAreaName(split[i]);
//                    List<TSysArea> areaList = tSysAreaService.selectTSysAreaList(tSysArea);
//                    if(areaList.size()>0){
//                        transportationCompany.setAreaCode(areaList.get(0).getAreaCode());
//                        transportationCompany.setAreaName(areaList.get(0).getAreaName());
//                    }
//                }
//
//            }







//            for (int i = 0; i < split.length; i++) {
//                System.out.println(split[i]);
//                // 省
//                if(i==0){
//                    TSysProvince tSysProvince = new TSysProvince();
//                    tSysProvince.setProvinceName(split[i]);
//                    List<TSysProvince> provinceList = tSysProvinceService.selectTSysProvinceList(tSysProvince);
//                    if(provinceList.size()>0){
//                        transportationCompany.setProvinceCode(provinceList.get(0).getProvinceCode());
//                        transportationCompany.setProvinceName(provinceList.get(0).getProvinceName());
//                    }
//                }else if(i==1){ //市
//                    TSysCity tSysCity = new TSysCity();
//                    tSysCity.setCityName(split[i]);
//                    List<TSysCity> cityList = tSysCityService.selectTSysCityList(tSysCity);
//                    if(cityList.size()>0){
//                        transportationCompany.setCityCode(cityList.get(0).getCityCode());
//                        transportationCompany.setCityName(cityList.get(0).getCityName());
//                    }
//
//                }else if(i==2){//区
//                    TSysArea tSysArea = new TSysArea();
//                    tSysArea.setAreaName(split[i]);
//                    List<TSysArea> areaList = tSysAreaService.selectTSysAreaList(tSysArea);
//                    if(areaList.size()>0){
//                        transportationCompany.setAreaCode(areaList.get(0).getAreaCode());
//                        transportationCompany.setAreaName(areaList.get(0).getAreaName());
//                    }
//                }
//
//            }
        }

        return transportationCompany;
    }
}
